package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhs.storage.bean.Car;
import com.hhs.storage.bean.CarFleet;
import com.hhs.storage.bean.CarLog;
import com.hhs.storage.bean.InputParam;
import com.hhs.storage.dao.CarLogDao;
import com.hhs.storage.service.CarFleetService;
import com.hhs.storage.service.CarLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.service.CarService;
import com.hhs.storage.util.SysUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhs
 * @since 2023-03-15
 */
@Service
@Transactional
public class CarLogServiceImpl extends ServiceImpl<CarLogDao, CarLog> implements CarLogService {
    @Resource
    private CarService carService;
    @Resource
    private CarFleetService carFleetService;
    /**
     * 添加日志
     * @param carId
     * @param operation
     * @param reason
     * @param creator
     */
    public void add(String carId,Integer operation,String reason,String creator){
        CarLog log=new CarLog();
        log.setId(SysUtil.UUID());
        log.setCarId(carId);
        log.setOperation(operation);
        log.setReason(reason);
        log.setCreator(creator);
        log.setCreateTime(LocalDateTime.now());
        this.save(log);
    }
    /**
     * 查询表格数据
     */
    @Override
    public Map<String, Object> getAll(CarLog carLog, InputParam inputParam) {
        QueryWrapper ew = new QueryWrapper<CarLog>();
        //日期查询格式转换
        if (inputParam.getStartDate()!=null){
            ew.ge("create_time",inputParam.getStartDate().atStartOfDay());
        }
        if (inputParam.getEndDate()!=null){
            ew.le("create_time",LocalDateTime.of(inputParam.getEndDate(), LocalTime.MAX));
        }
        //日志类型查询
        if (carLog.getOperation()!=null){
            ew.eq("operation",carLog.getOperation());
        }
        //车辆id查询
        if (StringUtils.isNotEmpty(carLog.getCarId())){
            ew.eq("car_id",carLog.getCarId());
        }
        //车牌号查询
        if (StringUtils.isNotEmpty(inputParam.getLicencePlate())){
            ew.inSql("car_id","select id from car where licence_plate like '%"+inputParam.getLicencePlate()+"%'");
        }
        //车队id查询
        if (StringUtils.isNotEmpty(inputParam.getCarFleetId())){
            ew.inSql("car_id","select id from car where car_fleet_id='"+inputParam.getCarFleetId()+"'");
        }
        Page<CarLog> result = this.page(new Page<CarLog>(
                inputParam.getPage(),
                inputParam.getSize()),ew);
        List<CarLog> logList=result.getRecords();
        //查询日志对应的车辆和车队信息
        for (int i = 0; i <logList.size() ; i++) {
            CarLog log=logList.get(i);
            Car car=carService.getById(log.getCarId());
            log.setCar(car);
            if (StringUtils.isNotEmpty(car.getCarFleetId())){
                CarFleet carFleet=carFleetService.getById(car.getCarFleetId());
                car.setCarFleet(carFleet);
            }
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", logList);
        map.put("total",result.getTotal());
        map.put("code", 1);
        return map;
    }
}
